<?php

/*
 * core/output.php
 *
 * Handle output to client as raw, json, ...other?
 */

abstract class OutputGenerator
{
	private $strid = '';								//!< string identification
	private $name = '';									//!< generator full name

	/*
	 * getOutput()
	 * to overload!
	 * called by Output to retreive the output for this system
	 * return what ever (normally...)
	 */
	public abstract function getOutput();

	/*
	 * constructor()
	 */
	public function __construct( $strid, $name )
	{
		$this->strid = $strid;
		$this->name = $name;

		Output::inst()->registerGenerator( $strid, $this );
	}

	public function getStrID()
	{
		return $this->strid;
	}

	public function getName()
	{
		return $this->name;
	}
};

/*
 * class Output
 *
 */
class Output
{
	private static $instance = null;

	private $generators = array();

	private $outputMethod = 'outputHTML';

	public function __construct()
	{
		Output::$instance =& $this;

		$this->setMode( Conf::$c['output']['type'] );
	}

	public static function &inst()
	{
		return Output::$instance;
	}

	public function setMode( $mode )
	{
		if( $mode == 'HTML' )
			$this->outputMethod = 'outputHTML';
		else if( $mode == 'JSON' )
			$this->outputMethod = 'outputJSON';
		else
			die( 'Output::setMode: unknown mode ('.$mode.')' );
	}

	public function registerGenerator( $strid, $generator )
	{
		if( array_key_exists($strid, $this->generators) )
		{
			die( 'Output::registerCollector: generator ('.$strid.') already registered.' );
		}

		$this->generators[$strid] = $generator;
	}

	public static function go()
	{
		Output::$instance->output();
	}

	public function output()
	{
		$mth =& $this->outputMethod;
		$this->$mth();
		die();
	}

	public function outputHTML()
	{
		foreach( $this->generators  as $strid => $generator )
		{
			$s = print_r( $generator->getOutput(), $return=true );
			$s = str_replace( "\n", "<br>\n", $s );

			print( '<p style="color:green; font-size:30px; text-decoration:underline"> '.$generator->getName().' ('.$strid.') </p>'."\n" );
			print( '<p style="border:1px solid black; width:97%; padding:20px">' . "\n" . $s . "\n" . '</p>' . "\n" );
		}
	}

	public function outputJSON()
	{
		$hsent = headers_sent( $file, $line );

		if( $hsent )
		{
			$debug = '';
			if( Conf::$c['debug']['active'] )
				$debug = '(file: '.$file.' - line: '.$line.')';

			print( '<p style="font-size:16pt"> Header already sent '.$debug.'. Printing html output. </p>' );
			$this->outputHTML();
			return;
		}

		$output = array();
		foreach( $this->generators as $strid => $generator )
		{
			$output[ $strid ] = $generator->getOutput();
		}

		header('Content-Type: text/javascript');
		header('Cache-Control: no-cache');
		header('Pragma: no-cache');

		print( '[' . json_encode($output) . ']' );
	}
};

// instanciate Output System
new Output();

/*
 * LogOutputGenerator class
 * Helper class so we don't have to include output.php before log.php, neither create a single file output_generator.php
 */
class LogOutputGenerator extends OutputGenerator
{
	public function __construct()
	{
		parent::__construct( 'log', 'Log' );
	}

	public function getOutput()
	{
		// return array of array( 'class'=>null, 'method'=>null, 'file'=>null, 'line'=>null, 'type'=>null, 'msg'=>null )
		return Log::inst()->getLogs();
	}
};
new LogOutputGenerator();


?>
